package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.ContactActivity;
import com.zzptc.zzsgz.bdsjws.activities.OneKeySOSActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SosAdapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.utils.MyUtils;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/27.
 */

public class TwoFragment extends BaseFragmentbase {

    @BindView(R.id.et_insert)
    EditText etInsert;

    @BindView(R.id.bt_contect)
    Button btContect;

    @BindView(R.id.iv_contect)
    ImageView ivContect;
    Unbinder unbinder;
    @BindView(R.id.rv_yijianqiujiu)
    RecyclerView rvYijianqiujiu;
    Unbinder unbinder1;
    SosAdapter sosAdapter;
    @BindView(R.id.twofrag_wancheng)
    Button twofragWancheng;
    Unbinder unbinder2;

    @Override
    public int getXML() {
        return R.layout.twofragment;
    }

    @Override
    public void initView() {
        etInsert.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                ivContect.setVisibility(View.GONE);
                btContect.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initDate() {

    }


    @OnClick(R.id.iv_contect)
    public void onViewClicked(View view) {
        startActivity(new Intent(getActivity(), ContactActivity.class));
    }


    @OnClick(R.id.bt_contect)
    public void onYesClicked() {
        //先判断电话号码的长度  并且获取电话号码
        String tel = etInsert.getText().toString().trim();
        if (tel.length() == 11 && tel.matches("^1[3578]\\d{9}$")) {
            //放入集合中  问题：集合哪儿创建
            //数据来源：一是添加的紧急联系人 二是 添加的联系人
            List<Contact> listsos = MyApplication.getSoslist();
            Contact contact = new Contact();
            contact.setTelnum(tel);
            if (listsos.size() < 3) {
                listsos.add(contact);
            } else {
                Toast.makeText(getActivity(), "已经有三人了", Toast.LENGTH_SHORT).show();
            }

            //还要一个适配器 （重新构造一个）     数据源   布局管理器
            //首先构建项的布局
            sosAdapter = new SosAdapter(listsos);
            rvYijianqiujiu.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvYijianqiujiu.setAdapter(sosAdapter);


        }
        //判断电话号码的长度
        etInsert.setText("");//清空
        ivContect.setVisibility(View.VISIBLE);
        btContect.setVisibility(View.GONE);
    }

    @Override
    //当碎片出现时就会调用
    public void onResume() {
        super.onResume();
        //Toast.makeText(getActivity(), "==============", Toast.LENGTH_SHORT).show();
        if (sosAdapter != null) {
            sosAdapter.notifyDataSetChanged();
        }
    }



    @OnClick(R.id.twofrag_wancheng)
    public void onViewClicked() {
        //跳转之前：获得求救电话（放入数据库）   电话内容（共享首选项） 定位（服务）
        SharedPreferences share= MyUtils.getpreferences();
        //String  sosInfo=share.getString("sos",null);
        SharedPreferences.Editor editor= share.edit();//得到编辑器
        editor.putString("sos","上课测试请勿当真。我遇到危险，请求帮助；");
        editor.commit();
        //求救电话放入数据库  并不是增加数据 而是对已有的数据库的表增加一列 是否是紧急联系人

        for (int i=0;i<MyApplication.getSoslist().size();i++){
            Contact contact=MyApplication.getSoslist().get(i);
//            if(){
//
//            }
        }
        if(MyApplication.getSoslist().size()==0){
            Toast.makeText(getActivity(), "请添加联系人", Toast.LENGTH_SHORT).show();
        }else {
            //跳转
            if(getActivity() instanceof OneKeySOSActivity){
                ((OneKeySOSActivity) getActivity()).ToThreeFragment();
            }
        }


    }
}
